//
//  BVShoppingCartTool.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/12.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVShoppingCartTool.h"
#import "BVShoppingCart.h"
//#import "MJExtension.h"

@interface BVShoppingCartTool ()

@end

@implementation BVShoppingCartTool

- (void)fetchMyShoppingCartData:(void (^)(NSArray *dataArr))success {
    
    NSDictionary*dic =
    @{
      @"code":@"1",
      @"msg":@"\u8bdd\u9898\u8be6\u60c5",
      @"result":
          @[
              @{
                  @"id":@"123",
                  @"status":@"0",
                  @"img":@"",
                  @"is_sales":@"1",
                  @"title":@"黄焖JB饭",
                  @"unit":@"袋",
                  @"unit_price":@"1.1元/斤",
                  @"weight":@"5斤装",
                  @"all_price":@"￥5.5",
                  @"goods_num":@"1",
                  @"notice":@"注:此商品需在13:00之前购买！"
                  },
              @{
                  @"id":@"456",
                  @"status":@"1",
                  @"img":@"",
                  @"is_sales":@"1",
                  @"title":@"大白菜",
                  @"unit":@"袋",
                  @"unit_price":@"6.0元/斤",
                  @"weight":@"6斤装",
                  @"all_price":@"￥36.0",
                  @"goods_num":@"1",
                  @"notice":@""
                  },
              @{
                  @"id":@"789",
                  @"status":@"1",
                  @"img":@"",
                  @"is_sales":@"0",
                  @"title":@"大白菜",
                  @"unit":@"袋",
                  @"unit_price":@"0.5元/斤",
                  @"weight":@"6斤装",
                  @"all_price":@"￥3.0",
                  @"goods_num":@"1",
                  @"notice":@"注:此商品没人限购1件！"
                  },
              @{
                  @"id":@"1011",
                  @"status":@"0",
                  @"img":@"",
                  @"is_sales":@"1",
                  @"title":@"大白菜",
                  @"unit":@"袋",
                  @"unit_price":@"1.0元/斤",
                  @"weight":@"6斤装",
                  @"all_price":@"￥6.0",
                  @"goods_num":@"1",
                  @"notice":@"注:女子被抢后叫住劫匪：你把这盒月饼也拿走吧"
                  
                  },
              @{
                  @"id":@"555",
                  @"status":@"1",
                  @"img":@"",
                  @"is_sales":@"1",
                  @"title":@"大白菜",
                  @"unit":@"袋",
                  @"unit_price":@"6.1元/袋",
                  @"weight":@"6斤装",
                  @"all_price":@"￥6.1",
                  @"goods_num":@"1",
                  @"notice":@"注:女子被抢后叫住劫匪：你把这盒月饼也拿走吧"
                  }
              ]
      };
    
    NSArray *goodsArr = [BVShoppingCart mj_objectArrayWithKeyValuesArray:dic[@"result"]];
    if (success) {
        success(goodsArr);
    }
}

- (void)fetchShoppingCartData:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/GoodsAPI/c_get_shopcart",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"user_id"] = self.user.user_id;
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        NSArray *dataArr = [BVShoppingCart mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];
        success(dataArr);
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)addGoodsToShoppingCart:(NSString *)goodsId success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/GoodsAPI/c_update_shopcart",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"type"] = @"inc";
    params[@"id"] = goodsId;
    params[@"user_id"] = self.user.user_id;

    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        NSString *code = responseObject[@"code"];
        if (success) {
            success(code);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

- (void)minusGoodsToShoppingCart:(NSString *)goodsId success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure {
    NSString *url = [NSString stringWithFormat:@"%@/GoodsAPI/c_update_shopcart",PATH];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"type"] = @"dec";
    params[@"id"] = goodsId;
    params[@"user_id"] = self.user.user_id;
    
    [BVHttpRequest getWithUrlString:url parameters:params success:^(id responseObject) {
        NSString *code = responseObject[@"code"];
        if (success) {
            success(code);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

@end

